package edu.towson.greenteam.tab;
import edu.towson.greenteam.Iterator;

public abstract class TabItr implements Iterator<Tab> {
	public Tabs allTabs;
	public boolean hasNextItem = false;
	public Tab nextTab = null;
	public Tab lastTab = null;
	protected int index = 0;

	public abstract boolean canProceed(Tab testTab);

	public boolean hasNext() {
		return hasNextItem;
	}

	public Tab next() {
		Tab returnTab = nextTab;
		calcNext();
		return returnTab;
	}
	
	// override the calc next
	public void calcNext() {
		hasNextItem = false;
		while (!hasNextItem && (allTabs.get(index) != null)) {
			Tab tempTab = allTabs.get(index);
			if (tempTab != null) {
				// if it isn't discounted go to the next one
				if (canProceed(tempTab)) {
					if (lastTab == nextTab) {
						lastTab = null;
					}
					else {
						lastTab = nextTab;
					}
					nextTab = tempTab;
					hasNextItem = true;
					index++;
				} else {
					calcNext();
				}
			}
		}
	}
	
	public boolean hasPrevious() {
		return (lastTab == null);
	}

	public Tab previous() {
		return lastTab;
	}

}
